package cn.kgc.spring.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: mosin
 * @version: v1.0  2022/10/12
 * 通过注解的方式将类交给spring容器管理
 * @Component  基础注解
 * 衍生注解
 * @Repository  持久层的注解
 * @Service   业务层注解
 * @Controller  控制层注解
 */

@Component
public class Student {

    @Value("20")
    private Integer id;
    @Value("tom")
    private String name;



    /**
     *  Autowired  默认 byType 根据类型注入
     *  如果在spring容器中存在多个Teacher类型的实例
     *  会尝试根据 -> byName 进行注入 如果没有找到  抛出异常  找到则注入
     *
     *  解决方法：
     *   @Autowired   推荐
     *   @Qualifier("teacher")  指定注入bean的id
     *
     *
     *   @Resource  jdk 默认  byName  找不到  ->   byType  - >多个  抛出异常
     *   @Resource(name = "teacher")
     *   @Resource(type = Teacher.class)
     *
     */

    //@Autowired
    //@Qualifier("teacher")
    //@Resource(type = Teacher.class)
    private Teacher teacher;

    @Autowired
    @Qualifier("teacher")
    public void setTeacher(Teacher teacher) {

        System.out.println("set方法被调用");
        this.teacher = teacher;
    }


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teacher=" + teacher +
                '}';
    }
}
